﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Base.Dao
{
    public class BranchManageInfoDao : DaoService<CommonEntity>
    {
        public override string TableName => "T_ProductVersion";
        public override string IDColumnName => "ID";
        public BranchManageInfoDao() : base()
        {
            DBName = "TFAS";
        }
        public override List<string> ExceptColumnName => new List<string> { "Children" };
        public override CommonEntity ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            var data = base.ConvertToObject(myCol, row);
            return data;
        }
        /// <summary>
        /// 根据产品版本ID获取子系统与模块
        /// </summary>
        public List<CommonTree> GetProductModules(string productVersionID)
        {
            List<CommonTree> subsystems = new List<CommonTree>();
            List<CommonTree> modules = new List<CommonTree>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@productVersionID", TDbDataType.VarChar, productVersionID));
            var module = this.DB.ExecuteDataSet("select ID,Code,Name,SubSystemID from t_productmodule where productVersionID=@productVersionID", paramsList.ToArray());
            if (module != null && module.Tables.Count > 0 && module.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in module.Tables[0].Rows)
                {
                    CommonTree commonEntity = new CommonTree();
                    commonEntity.ID = Convert.ToString(row["ID"]);
                    commonEntity.Code = Convert.ToString(row["Code"]);
                    commonEntity.Name = Convert.ToString(row["Name"]);
                    commonEntity.ParentID = Convert.ToString(row["SubSystemID"]);
                    modules.Add(commonEntity);
                }
            }
            var subsystem = this.DB.ExecuteDataSet("select SubSystemID ID,Code,Name from t_productsubsystem where productVersionID=@productVersionID", paramsList.ToArray());
            if (subsystem != null && subsystem.Tables.Count > 0 && subsystem.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in subsystem.Tables[0].Rows)
                {
                    CommonTree commonEntity = new CommonTree();
                    commonEntity.ID = Convert.ToString(row["ID"]);
                    commonEntity.Code = Convert.ToString(row["Code"]);
                    commonEntity.Name = Convert.ToString(row["Name"]);
                    commonEntity.ParentID = productVersionID;
                    commonEntity.Children = modules.Where(t => t.ParentID == commonEntity.ID).ToList();
                    subsystems.Add(commonEntity);
                }
            }
            return subsystems;
        }
        /// <summary>
        /// 根据产品ID获取产品版本列表
        /// </summary>
        public List<CommonEntity> GetProductVersions(string productID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@productID", TDbDataType.VarChar, productID));
            var sql = @"SELECT ID,ProductVersion FROM T_ProductVersion where productID=@productID";
            var ds = this.DB.ExecuteDataSet(sql, paramsList.ToArray());
            List<CommonEntity> list = new List<CommonEntity>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    CommonEntity commonEntity = new CommonEntity();
                    commonEntity.ID = Convert.ToString(row["ID"]);
                    commonEntity.Code = Convert.ToString(row["ProductVersion"]);
                    commonEntity.Name = Convert.ToString(row["ProductVersion"]);
                    list.Add(commonEntity);
                }
            }
            return list;
        }
        /// <summary>
        /// 根据产品版本、子系统和模块获取新的版本下的信息
        /// </summary>
        public BranchManageInfo GetBranchManageInfo(string productVersionID, string subSystemCode, string moduleCode)
        {
            BranchManageInfo manageInfo = new BranchManageInfo();
            manageInfo.ProductVersionID = productVersionID;
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@productVersionID", TDbDataType.VarChar, productVersionID));
            paramsList.Add(this.DB.MakeInParam("@subSystemCode", TDbDataType.VarChar, subSystemCode));
            var subsystem = this.DB.ExecuteDataSet("select SubSystemID ID,Code,Name from t_productsubsystem where Code=@subSystemCode and productVersionID=@productVersionID", paramsList.ToArray());
            if (subsystem != null && subsystem.Tables.Count > 0 && subsystem.Tables[0].Rows.Count > 0)
            {
                CommonEntity subsystemObj = ConvertToObject(this.Columns, subsystem.Tables[0].Rows[0]);
                if(subsystemObj != null)
                {
                    manageInfo.SubSystemID = subsystemObj.ID;
                    manageInfo.SubSystemCode = subsystemObj.Code;
                    manageInfo.SubSystemName = subsystemObj.Name;
                    paramsList.Add(this.DB.MakeInParam("@moduleCode", TDbDataType.VarChar, moduleCode));
                    paramsList.Add(this.DB.MakeInParam("@subSystemID", TDbDataType.VarChar, subsystemObj.ID));
                    var module = this.DB.ExecuteDataSet("select ID,Code,Name from t_productmodule where Code=@moduleCode and subSystemID=@subSystemID and productVersionID=@productVersionID", paramsList.ToArray());
                    if (module != null && module.Tables.Count > 0 && module.Tables[0].Rows.Count > 0)
                    {
                        CommonEntity moduleObj = ConvertToObject(this.Columns, module.Tables[0].Rows[0]);
                        manageInfo.ModuleID = moduleObj.ID;
                        manageInfo.ModuleCode = moduleObj.Code;
                        manageInfo.ModuleName = moduleObj.Name;
                    }
                    else
                    {
                        throw new Exception("当前产品版本下没有对应的模块！");
                    }
                }
                else
                {
                    throw new Exception("当前产品版本下没有对应的子系统！");
                }
            }
            return manageInfo;
        }
    }
}
